package lg.minecraft.plugin.pokedex.item.builder;

import lg.minecraft.plugin.library.common.PlaceholderReplacer;
import lg.minecraft.plugin.library.item.ItemProperties;
import lg.minecraft.plugin.library.item.builder.AbstractItemBuilder;
import lg.minecraft.plugin.pokedex.reward.RewardStatus;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 奖励状态物品构建器
 * 根据奖励状态(RewardStatus)构建不同显示效果的物品
 *
 * @author langle__
 * @version 1.0
 */
public class RewardItemBuilder extends AbstractItemBuilder {

    private final ItemProperties notQualifiedProperties;
    private final ItemProperties qualifiedProperties;
    private final ItemProperties claimedProperties;

    /**
     * 构造奖励状态物品构建器
     *
     * @param notQualifiedProperties 未达成要求状态的物品配置
     * @param qualifiedProperties 可领取状态的物品配置
     * @param claimedProperties 已领取状态的物品配置
     */
    public RewardItemBuilder(ItemProperties notQualifiedProperties, ItemProperties qualifiedProperties, ItemProperties claimedProperties) {
        super(notQualifiedProperties);
        this.notQualifiedProperties = notQualifiedProperties;
        this.qualifiedProperties = qualifiedProperties;
        this.claimedProperties = claimedProperties;
    }

    @Override
    protected ItemStack build(Map<String, String> map, Object... objects) {
        RewardStatus status = (RewardStatus) objects[0];
        //noinspection unchecked
        List<String> rewardLoreList = (List<String>) objects[1];
        ItemProperties selected = switch (status) {
            case NOT_QUALIFIED -> notQualifiedProperties;
            case QUALIFIED -> qualifiedProperties;
            case CLAIMED -> claimedProperties;
        };
        ItemStack stack = new ItemStack(selected.getMaterial());
        ItemMeta meta = stack.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(PlaceholderReplacer.replaceInString(selected.getName(), map));
            List<String> loreList = new ArrayList<>();
            for (String lore : selected.getLore()) {
                if (lore.equalsIgnoreCase("{reward}")) {
                    loreList.addAll(PlaceholderReplacer.replaceInList(rewardLoreList, map));
                    continue;
                }
                loreList.add(PlaceholderReplacer.replaceInString(lore, map));
            }
            meta.setLore(loreList);
            selected.getEnchantments().forEach((enchantedEntry) -> meta.addEnchant(enchantedEntry.getEnchantment(), enchantedEntry.getLevel(), true));
            selected.getItemFlags().forEach(meta::addItemFlags);
            stack.setItemMeta(meta);
        }

        return stack;
    }

    public ItemStack build(Map<String, String> map, RewardStatus status, List<String> lore) {
        return build(map, (Object) status, lore);
    }

    public ItemStack build(Map<String, String> map, RewardStatus status) {
        return build(map, status, Collections.emptyList());
    }

}
